//
//  UCSeparatorLayout.swift
//  UICreate
//
//  Created by JY_NEW on 2022/8/31.
//

import Foundation

extension UCLayout {
    /// 分隔线位置
    public enum SeparatorPosition {
        case top(CGFloat)
        case bottom(CGFloat)
        case left(CGFloat)
        case right(CGFloat)
        case topToBottom(CGFloat)
        case bottomToTop(CGFloat)
        case leftToRight(CGFloat)
        case rightToLeft(CGFloat)
    }
    /// 针对上下左右四个间距添加分隔线，其他类型的Layout不支持
    public static func separator(_ position: SeparatorPosition, indent: CGFloat, _ settings: UCSetting<UIView>...) -> UCLayout {
        return .separator(position, leading: indent, trailing: indent, settings: settings)
    }
    /// 针对上下左右四个间距添加分隔线，其他类型的Layout不支持
    public static func separator(_ position: SeparatorPosition, leading: CGFloat = 0, trailing: CGFloat = 0, _ settings: UCSetting<UIView>...) -> UCLayout {
        return .separator(position, leading: leading, trailing: trailing, settings: settings)
    }
    /// 针对上下左右四个间距添加分隔线，其他类型的Layout不支持
    public static func separator(_ position: SeparatorPosition, leading: CGFloat, trailing: CGFloat, settings: [UCSetting<UIView>]) -> UCLayout {
        let inset: UIEdgeInsets
        switch position {
        case .top, .bottom, .topToBottom, .bottomToTop:
            inset = .init(top: 0, left: leading, bottom: 0, right: trailing)
        case .left, .right, .leftToRight, .rightToLeft:
            inset = .init(top: leading, left: 0, bottom: trailing, right: 0)
        }
        return .separator(position, inset: inset, settings: settings)
    }
    /// 针对上下左右四个间距添加分隔线，其他类型的Layout不支持
    public static func separator(_ position: SeparatorPosition, inset: UIEdgeInsets, _ settings: UCSetting<UIView>...) -> UCLayout {
        return .separator(position, inset: inset, settings: settings)
    }
    /// 针对上下左右四个间距添加分隔线，其他类型的Layout不支持
    public static func separator(_ position: SeparatorPosition, inset: UIEdgeInsets, settings: [UCSetting<UIView>]) -> UCLayout {
        let layout: UCLayout
        switch position {
        case .top(let value): layout = .top(value)
        case .bottom(let value): layout = .bottom(value)
        case .left(let value): layout = .left(value)
        case .right(let value): layout = .right(value)
        case .topToBottom(let value): layout = .topToBottom(value)
        case .bottomToTop(let value): layout = .bottomToTop(value)
        case .leftToRight(let value): layout = .leftToRight(value)
        case .rightToLeft(let value): layout = .rightToLeft(value)
        }
        let customLayout: ([UCLayout], [UCLayout]) -> UCLayout = { v2LOs, v1LOs in
            return .customize { v1, v2 in
                layout.apply(for: v1, to: v2)
                let separator = UIView().set(animated: 0, settings: settings)
                if let sv = v1.superview ?? v2.superview {
                    sv.addSubview(separator)
                    v2LOs.forEach { $0.apply(for: separator, to: v2) }
                    v1LOs.forEach { $0.apply(for: separator, to: v1) }
                }
            }
        }
        switch position {
        case .left:
            return customLayout([.left(inset.left)], [.rightToLeft(inset.right), .top(inset.top), .bottom(inset.bottom)])
        case .right:
            return customLayout([.right(inset.right)], [.leftToRight(inset.left), .top(inset.top), .bottom(inset.bottom)])
        case .top:
            return customLayout([.top(inset.top)], [.bottomToTop(inset.bottom), .left(inset.left), .right(inset.right)])
        case .bottom:
            return customLayout([.bottom(inset.bottom)], [.topToBottom(inset.top), .left(inset.left), .right(inset.right)])
        case .leftToRight:
            return customLayout([.leftToRight(inset.left)], [.rightToLeft(inset.right), .top(inset.top), .bottom(inset.bottom)])
        case .rightToLeft:
            return customLayout([.rightToLeft(inset.right)], [.leftToRight(inset.left), .top(inset.top), .bottom(inset.bottom)])
        case .topToBottom:
            return customLayout([.topToBottom(inset.top)], [.bottomToTop(inset.bottom), .left(inset.left), .right(inset.right)])
        case .bottomToTop:
            return customLayout([.bottomToTop(inset.bottom)], [.topToBottom(inset.top), .left(inset.left), .right(inset.right)])
        }
    }
}
